package yunquan.wang.base.process;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.core.io.Resource;

import yunquan.wang.base.spring.SpringContextHolder;

public class KnowledgeBaseBeanFactory implements FactoryBean<KnowledgeBase> {
	private String filePath;

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	@Override
	public KnowledgeBase getObject() throws Exception {
		Resource[] resources = SpringContextHolder.getApplicationContext()
				.getResources(filePath);
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();
		for (Resource resource : resources) {
			kbuilder.add(ResourceFactory.newInputStreamResource(resource
					.getInputStream()), ResourceType.BPMN2);
		}
		return kbuilder.newKnowledgeBase();

	}

	@Override
	public Class<?> getObjectType() {
		return KnowledgeBase.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}
